package com.ruoyi.web.controller.dingtalkReport;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.dto.FSMonitorData;
import com.ruoyi.system.dto.FSRecordData;
import com.ruoyi.system.dto.FlowViewData;
import com.ruoyi.system.domain.FSRecord;
import com.ruoyi.system.dto.WeightData;
import com.ruoyi.system.service.IFSRecordService;
import com.ruoyi.system.vo.FSRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 设备报警信息-产量报警信息-流量图表-监控信息
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/fsRecord")
public class FSRecordController extends BaseController
{

    @Autowired
    private IFSRecordService ifsRecordService;


    @PostMapping("/getFSRecordList")
    @ResponseBody
    @DS("fsRecord")
    public AjaxResult getFSRecordList(@RequestBody FSRecordVO fsRecordVO) throws Exception
    {
        List<FSRecordData> fsRecordList = ifsRecordService.selectFSRecords(fsRecordVO);
        return success(fsRecordList);
    }

    @PostMapping("/getFlowViewList")
    @ResponseBody
    @DS("fsRecord")
    public AjaxResult getFlowViewList(@RequestBody FSRecordVO fsRecordVO) throws Exception
    {
        List<FlowViewData> flowViewList = ifsRecordService.getFlowViewList(fsRecordVO);
        Map<String, List<FlowViewData>> map = flowViewList.stream().collect(
                Collectors.groupingBy(
                        score -> score.getMachineNo()
                ));
        return success(map);
    }

    @GetMapping("/getFSMonitorList")
    @ResponseBody
    @DS("fsRecord")
    public AjaxResult getFSMonitorList() throws Exception
    {
        List<FSMonitorData> flowViewList = ifsRecordService.getFSMonitorList();
        return success(flowViewList);
    }

    @PostMapping("/getWeightCount")
    @ResponseBody
    @DS("fsRecord")
    public AjaxResult getWeightCount(@RequestBody FSRecordVO fsRecordVO) throws Exception
    {
        List<FlowViewData> flowViewList = ifsRecordService.getWeightCount(fsRecordVO);
        Map<String, List<FlowViewData>> map = flowViewList.stream().collect(
                Collectors.groupingBy(
                        score -> score.getMachineNo()
                ));
        Map<String, BigDecimal> countMap = new HashMap<>();
        List<WeightData> weightDataArrayList = new ArrayList<>();
        map.forEach((k,v)->{
            System.out.println(k);
            BigDecimal count = BigDecimal.ZERO;
            for(int i = 0; i+1 < v.size(); i++) {
                BigDecimal weightI = v.get(i).getWeight();
                BigDecimal weightJ = v.get(i+1).getWeight();
                if(weightJ.compareTo(weightI) == 1 && weightJ.compareTo(BigDecimal.ONE) == 1) {
                    count = count.add(v.get(i+1).getWeight());
                }
            }
            WeightData weightData = new WeightData();
            weightData.setWeight(count);
            weightData.setMachineNo(k);
            weightDataArrayList.add(weightData);
        });
        return success(weightDataArrayList);
    }
}
